﻿using Sustainalytics.ClientDW.DataAccessLayer;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.Entities;
using Sustainalytics.Entities.ConnectionStrings;
using Sustainalytics.Entities.ProductInvolvement;
using Sustainalytics.ESG.Entities;
using Sustainalytics.Synchronization.Interface;
using Sustainalytics.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Sustainalytics.Universes.Synchronization
{
    public class AssureFeatureUniversesSyncStep : ISyncStep
    {
        public SyncStepOrder ExecutionOrder => SyncStepOrder.AssureFeatureUniverses;

        public SyncContext Start(SyncContext syncContext)
        {
            try
            {
                if (syncContext.CompanyUniverses.IsNullOrEmpty())
                {
                    string clientUniversesConnectionString = syncContext.GAConnectionStrings[ConnectionStringType.ClientUniverses.ToString()];
                    var universesRepository = UniversesRepository.GetUniversesRepository(clientUniversesConnectionString);
                    syncContext.CompanyUniverses = universesRepository.GetAllUniverseCompany(); //add esg profile filtering
                    syncContext.Log("Acquired companies universes");
                }
                StartInParallelAsync(syncContext).Wait();
                syncContext.Status = SyncOperationStatus.Succeeded;
            }
            catch (Exception ex)
            {
                syncContext.Status = SyncOperationStatus.Failed;
                syncContext.Log(ex.ToString());
            }
            return syncContext;
        }

        private async Task StartInParallelAsync(SyncContext syncContext)
        {
            var handlers = new List<IAssureFeatureUniverse>()
                {
                    new AssureFeatureUniverseByIdHandler<CorporateData>(syncContext.GAConnectionStrings[ConnectionStringType.ClientDW.ToString()]),

                    new AssureFeatureUniverseByIdHandler<FinancialData>(syncContext.GAConnectionStrings[ConnectionStringType.ClientDW.ToString()]),
                    new AssureFeatureUniverseByIdHandler<CompanyKeyESGIssue>(syncContext.GAConnectionStrings[ConnectionStringType.ClientDW.ToString()]),
                    new AssureFeatureUniverseByIdHandler<GCOverallCompanySummary>(syncContext.GAConnectionStrings[ConnectionStringType.ClientDW.ToString()]),
                    new AssureFeatureUniverseByIdHandler<GCOverallCompanySummaryExtended>(syncContext.GAConnectionStrings[ConnectionStringType.ClientDW.ToString()]),

                    new AssureFeatureUniverseByCompanyIdHandler<RPControversyIndicator>(syncContext.GAConnectionStrings[ConnectionStringType.ClientDW.ToString()]),
                    new AssureFeatureUniverseByCompanyIdHandler<EventIndicator>(syncContext.GAConnectionStrings[ConnectionStringType.ClientDW.ToString()]),

                    new AssureFeatureUniverseByIdHandler<RPCompanyESGComputationResult>(syncContext.GAConnectionStrings[ConnectionStringType.ClientDW.ToString()]),
                    new AssureFeatureUniverseByCompanyIdHandler<RPIndicatorExtended>(syncContext.GAConnectionStrings[ConnectionStringType.ClientDW.ToString()]),
                };

            foreach (var handler in handlers)
            {
                syncContext.Log("Start setting universe id's for collection " + handler.CollectionName);
                var stopwatch = new Stopwatch();
                stopwatch.Start();
                await handler.UpdateUniverseIdsAsync(syncContext.CompanyUniverses);
                stopwatch.Stop();
                syncContext.Log("Finish setting universe id's for collection " + handler.CollectionName + " in " + stopwatch.Elapsed);
            }
        }
    }
}
